package com.qijian.maindata.domain.vo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qijian.common.annotation.Excel;
import com.qijian.maindata.domain.ProcessOrderUsers;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static java.math.RoundingMode.HALF_UP;

/**
 * 工序工单派单人员对象
 *
 * @author qijian
 * @date 2021-10-09
 */

@EqualsAndHashCode(callSuper = true)
@Data
@ToString
public class ProcessOrderUsersVo extends ProcessOrderUsers {
    @JsonSerialize(using = ToStringSerializer.class)
    private Long workUserId;
    //    @Excel(name = "作业人员", sort = 13)
//    private String nickName;
//    @Excel(name = "作业人员所在车间", sort = 6)
//    private String deptName;
    @Excel(name = "工单管理人", sort = 12)
    private String managerNickName;
    @Excel(name = "管理人员所在车间", sort = 5)
    private String managerDeptName;
    @Excel(name = "物料名称", sort = 10)
    private String itemName;
    @JsonSerialize(using = ToStringSerializer.class)
    private Long processId;
    @Excel(name = "工序", sort = 11)
    private String processName;
    @Excel(name = "工单编号", sort = 1)
    private String workCode;
    @Excel(name = "派工类型", dictType = "process_order_type", sort = 3)
    private String workType;
    @Excel(name = "NCR编号", sort = 4)
    private String ncrCode;
    private Date workStartTime;
    private Date workEndTime;
    @Excel(name = "是否需要检验")
    private Boolean isCheck;
    @JsonSerialize(using = ToStringSerializer.class)
    private Long projectId;
    @Excel(name = "项目编号", sort = 7)
    private String projectNo;
    @Excel(name = "产品名称", sort = 9)
    private String projectName;
    @Excel(name = "机台号", sort = 8)
    private String machineNo;
    @Excel(name = "作业人员手机号", sort = 14)
    private String phone;

    @TableField(exist = false)
    @JsonIgnore
    @Excel(name = "日期", sort = 2)
    @ApiModelProperty(value = "excel导出日期")
    private String workStartEndTime;

    @Excel(name = "创建时间", dateFormat = "yyyy-MM-dd HH:mm:ss", sort = 19)
    private Date processOrderCreateTime;

    @Excel(name = "派单时间", dateFormat = "yyyy-MM-dd HH:mm:ss", sort = 20)
    private Date processOrderUserCreateTime;

    @Excel(name = "系统工时(h)\n" +
            "(报工-接单)", sort = 15)
    @JsonIgnore
    @TableField(exist = false)
    private BigDecimal systemTimes;

    private String processTypeName;

    private BigDecimal workCost;

    private BigDecimal workTime;

    private Integer workNumber;

    private String drawingNo;

    private String partNo;

    @Excel(name = "项目经理", sort = 21)
    private String projectManager;

    private List<ProcessOrderUsersVo> children;

    private Boolean delayFlag;

    private Boolean onLineFlag;

    //工序对应的工艺文件数量
    private Integer processFileCnt;

    private String costFactory;

    private boolean targetUser;
    public String getWorkStartEndTime() {
        if (this.getWorkStartTime() == null || this.getWorkEndTime() == null) return null;
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return fm.format(this.getWorkStartTime()) + " 至 " + fm.format(this.getWorkEndTime());
    }

    public BigDecimal getSystemTimes() {
        if (this.getReportTime() == null || this.getReceiveTime() == null)
            return null;
        long difference = this.getReportTime().getTime() - this.getReceiveTime().getTime();
        return new BigDecimal(difference)
                .divide(new BigDecimal(1000), 4, HALF_UP)
                .divide(new BigDecimal(60), 4, HALF_UP)
                .divide(new BigDecimal(60), 2, HALF_UP);
    }

    public Boolean getDelayFlag() {
        if (this.getWorkEndTime() == null) {
            return null;
        }
        long difference = System.currentTimeMillis() - this.getWorkEndTime().getTime();
        return difference > 0 ;
    }

    public Date getProcessOrderUserCreateTime() {
        return this.getCreateTime();
    }
}
